import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtCourseInfoApi } from '#/api/cmt/course/info';

import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { DICT_TYPE } from '@vben/constants';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getCourseCategoryOptions } from '#/api/cmt/course/category';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'categoryId',
      label: '课程分类',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择课程分类',
        api: getCourseCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'title',
      label: '课程标题',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入课程标题',
      },
    },
    {
      fieldName: 'coverImage',
      label: '封面图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'teacherPersonId',
      label: '讲师人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入讲师人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'teacherName',
      label: '讲师名称',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入讲师名称',
      },
    },
    {
      fieldName: 'teacherTitle',
      label: '讲师职称',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入讲师职称',
      },
    },
    {
      fieldName: 'teacherAvatar',
      label: '讲师头像',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'difficulty',
      label: '难度',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '初级', value: 1 },
          { label: '中级', value: 2 },
          { label: '高级', value: 3 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'lessonCount',
      label: '课时数',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入课时数',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'duration',
      label: '总时长(分钟)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入总时长',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'description',
      label: '课程简介',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入课程简介',
      },
    },
    {
      fieldName: 'content',
      label: '课程详情',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 4,
        placeholder: '请输入课程详情',
      },
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_BOOLEAN, 'number'),
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isHot',
      label: '是否热门',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_BOOLEAN, 'number'),
      },
      defaultValue: 0,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入商户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'categoryId',
      label: '课程分类',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请选择课程分类',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'title',
      label: '课程标题',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入课程标题',
      },
    },
    {
      fieldName: 'difficulty',
      label: '难度',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '初级', value: 1 },
          { label: '中级', value: 2 },
          { label: '高级', value: 3 },
        ],
        placeholder: '请选择难度',
      },
    },
    {
      fieldName: 'isRecommend',
      label: '是否推荐',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_BOOLEAN, 'number'),
        placeholder: '请选择是否推荐',
      },
    },
    {
      fieldName: 'isHot',
      label: '是否热门',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_BOOLEAN, 'number'),
        placeholder: '请选择是否热门',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtCourseInfoApi.CourseInfo>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '课程ID',
      minWidth: 80,
    },
    {
      field: 'title',
      title: '课程标题',
      minWidth: 200,
    },
    {
      field: 'coverImage',
      title: '封面图片',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'categoryId',
      title: '课程分类',
      minWidth: 100,
    },
    {
      field: 'teacherName',
      title: '讲师名称',
      minWidth: 100,
    },
    {
      field: 'teacherTitle',
      title: '讲师职称',
      minWidth: 100,
    },
    {
      field: 'difficulty',
      title: '难度',
      minWidth: 80,
      slots: { default: 'difficulty' },
    },
    {
      field: 'lessonCount',
      title: '课时数',
      minWidth: 80,
    },
    {
      field: 'duration',
      title: '总时长(分钟)',
      minWidth: 120,
    },
    {
      field: 'learnCount',
      title: '学习人数',
      minWidth: 100,
    },
    {
      field: 'completeCount',
      title: '完成人数',
      minWidth: 100,
    },
    {
      field: 'rating',
      title: '课程评分',
      minWidth: 100,
    },
    {
      field: 'isRecommend',
      title: '是否推荐',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_BOOLEAN },
      },
    },
    {
      field: 'isHot',
      title: '是否热门',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_BOOLEAN },
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
